<?php
// +----------------------------------------------------------------------
// | 众联TK架构 [ ZoomTk ]
// +----------------------------------------------------------------------
// | 版权所有 2017~2037 成都众联思索科技有限公司 [ http://www.zoomtk.com ]
// +----------------------------------------------------------------------
// | 官方网站: http://www.sopvip.com
// +----------------------------------------------------------------------
// | 开源协议 ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------

namespace app\mall\admin;

use app\admin\controller\Admin;
use app\common\builder\TKuitpl;
use think\Model;
use think\Cache;
use think\Db;
/**
 * 商品管理中心
 * @package app\mall\admin
 */
class Index extends Admin
{
    /**
     * 首页
     * @author Sunday <153788231@qq.com>
     * @return mixed
     */
    public function goods()
    {
        $model_goods = Model('goods');
        $where = array('goods_name', 'goods_commonid', 'store_name', 'brand_name', 'cate_id', 'goods_state', 'goods_verify','type');
        $map= array('query','sortname','sortorder');
        $data = checkFields($this->request->get(), $where);
        $_POST = checkFields($this->request->port(), $map);
        $condition = array();
        if ($data['goods_name'] != '') {
            $condition['goods_name'] = array('like', '%' . $data['goods_name'] . '%');
        }
        if ($data['goods_commonid'] != '') {
            $condition['goods_commonid'] = array('like', '%' . $data['goods_commonid'] . '%');
        }
        if ($data['store_name'] != '') {
            $condition['store_name'] = array('like', '%' . $data['store_name'] . '%');
        }
        if ($data['brand_name'] != '') {
            $condition['brand_name'] = array('like', '%' . $data['brand_name'] . '%');
        }
        if (intval($data['cate_id']) > 0) {
            $condition['gc_id'] = intval($data['cate_id']);
        }
        if ($data['goods_state'] != '') {
            $condition['goods_state'] = $data['goods_state'];
        }
        if ($data['goods_verify'] != '') {
            $condition['goods_verify'] = $data['goods_verify'];
        }
        if ($_POST['query'] != '') {
            $condition[$_POST['qtype']] = array('like', '%' . $_POST['query'] . '%');
        }
        $order = '';
        $param = array('goods_commonid', 'goods_name', 'goods_price', 'goods_state', 'goods_verify', 'goods_image', 'goods_jingle', 'gc_id'
        , 'gc_name', 'store_id', 'store_name', 'is_own_shop', 'brand_id', 'brand_name', 'goods_addtime', 'goods_marketprice', 'goods_costprice'
        , 'goods_freight', 'is_virtual', 'virtual_indate', 'virtual_invalid_refund', 'is_fcode', 'is_appoint', 'appoint_satedate'
        , 'is_presell', 'presell_deliverdate'
        );
        if (in_array($_POST['sortname'], $param) && in_array($_POST['sortorder'], array('asc', 'desc'))) {
            $order = $_POST['sortname'] . ' ' . $_POST['sortorder'];
        }
        switch ($data['type']) {
            // 禁售
            case 'lockup':
                $goods_list = $model_goods->getGoodsCommonLockUpList($condition, '*',  $order);
                break;
            // 等待审核
            case 'waitverify':
                $goods_list = $model_goods->getGoodsCommonWaitVerifyList($condition, '*', $order);
                break;
            // 全部商品
            default:
                $goods_list = $model_goods->getGoodsCommonList($condition, '*', $order);
                break;
        }
        $page = $goods_list->render();
        // 库存
        $storage_array = $model_goods->calculateStorage($goods_list);
        // 商品状态
        $goods_state = $this->getGoodsState();
        // 审核状态
        $verify_state = $this->getGoodsVerify();

        foreach ($goods_list as $value) {
            $param = array();
  //          $operation = '';
//            switch ($data['type']) {
//                // 禁售
//                case 'lockup':
//                    $operation .= "<a class='btn red' href='javascript:void(0);' onclick=\"fg_del('" . $value['goods_commonid'] . "')\"><i class='fa fa-trash-o'></i>删除</a>";
//                    break;
//                // 等待审核
//                case 'waitverify':
//                    $operation .= "<a class='btn orange' href='javascript:void(0);' onclick=\"fg_verify('" . $value['goods_commonid'] . "')\"><i class='fa fa-check-square'></i>审核</a>";
//                    break;
//                // 全部商品
//                default:
//                    $operation .= "<a class='btn red' href='javascript:void(0);' onclick=\"fg_lonkup('" . $value['goods_commonid'] . "')\"><i class='fa fa-ban'></i>下架</a>";
//                    break;
//            }
//            $operation .= "<span class='btn'><em><i class='fa fa-cog'></i>设置 <i class='arrow'></i></em><ul>";
//            $operation .= "<li><a href='" . url('goods/index', array('goods_id' => $storage_array[$value['goods_commonid']]['goods_id'])) . "' target=\"_blank\">查看商品详细</a></li>";
//            $operation .= "<li><a href='javascript:void(0)' onclick=\"fg_sku('" . $value['goods_commonid'] . "')\">查看商品SKU</a></li>";
//            $operation .= "</ul>";
//            $param['operation'] = $operation;
            $param['goods_commonid'] = $value['goods_commonid'];
            $param['goods_name'] = $value['goods_name'];
            $param['goods_price'] = priceFormat($value['goods_price']);
            $param['goods_state'] = $goods_state[$value['goods_state']];
            $param['goods_verify'] = $verify_state[$value['goods_verify']];
        //    $param['goods_image'] = "<a href='javascript:void(0);' class='pic-thumb-tip' onMouseOut='toolTip()' onMouseOver='toolTip(\"<img src=" . thumb($value, '60') . ">\")'><i class='fa fa-picture-o'></i></a>";
            $param['goods_jingle'] = $value['goods_jingle'];
            $param['gc_id'] = $value['gc_id'];
            $param['gc_name'] = $value['gc_name'];
            $param['store_id'] = $value['store_id'];
            $param['store_name'] = $value['store_name'];
            $param['is_own_shop'] = $value['is_own_shop'] == 1 ? '平台自营' : '入驻商户';
            $param['brand_id'] = $value['brand_id'];
            $param['brand_name'] = $value['brand_name'];
            $param['goods_addtime'] = date('Y-m-d', $value['goods_addtime']);
            $param['goods_marketprice'] = priceFormat($value['goods_marketprice']);
            $param['goods_costprice'] = priceFormat($value['goods_costprice']);
            $param['goods_freight'] = $value['goods_freight'] == 0 ? '免运费' : priceFormat($value['goods_freight']);
            $param['goods_storage'] = $storage_array[$value['goods_commonid']]['sum'];
            $param['is_virtual'] = $value['is_virtual'] == '1' ? '<span class="yes"><i class="fa fa-check-circle"></i>是</span>' : '<span class="no"><i class="fa fa-ban"></i>否</span>';
            $param['virtual_indate'] = $value['is_virtual'] == '1' && $value['virtual_indate'] > 0 ? date('Y-m-d', $value['virtual_indate']) : '--';
            $param['virtual_invalid_refund'] = $value['is_virtual'] == '1' ? ($value['virtual_invalid_refund'] == 1 ? '<span class="yes"><i class="fa fa-check-circle"></i>是</span>' : '<span class="no"><i class="fa fa-ban"></i>否</span>') : '--';
            $param['is_appoint'] = $value['is_appoint'] == '1' ? '<span class="yes"><i class="fa fa-check-circle"></i>是</span>' : '<span class="no"><i class="fa fa-ban"></i>否</span>';
            $param['appoint_satedate'] = $value['is_appoint'] == '1' && $value['appoint_satedate'] > 0 ? date('Y-m-d', $value['appoint_satedate']) : '--';
            $_data[$value['goods_commonid']] = $param;
        }
        $list_tab = [
            'tab1' => ['title' => '全部商品', 'url' => url('goods')],
            'tab2' => ['title' => '等待审核', 'url' => url('waitverify_list')],
            'tab3' => ['title' => '商品设置', 'url' => url('index', ['waitverify_list' => 'tab3'])],
        ];
        // 使用TKuitpl快速创建数据表格
        return TKuitpl::make('table')
            ->setPageTitle('商品管理')// 设置页面标题
            ->setTabNav($list_tab,  'tab1')
            ->setPrimaryKey('goods_commonid') // 设置主键名为username
            ->setSearch(['id' => 'ID', 'username' => '用户名', 'email' => '邮箱'])// 设置搜索参数
            ->addColumns([ // 批量添加列
                ['goods_commonid', 'SPU'],
                ['goods_image', '商品图片','picture'],
                ['goods_name', '商品名称'],
                ['goods_price', '商品价格'],
                ['store_id', '商家ID'],
                ['store_name', '商家名称'],
                ['is_own_shop', '店铺类型'],
                ['gc_id', '分类ID'],
                ['gc_name', '分类名称'],
                ['status', '状态', 'switch'],
                ['right_button', '操作', 'btn']
            ])
            ->addTopButtons('add','enable,disable,delete')// 批量添加顶部按钮
            ->addRightButtons('edit,delete')// 批量添加右侧按钮
            ->setRowList($_data)// 设置表格数据
            ->setPages($page)// 设置分页数据
            ->fetch(); // 渲染页面
    }
    /**
     * 商品状态
     * @return multitype:string
     */
    private function getGoodsState() {
        return array('1' => '出售中', '0' => '仓库中', '10' => '违规下架');
    }

    private function getGoodsVerify() {
        return array('1' => '通过', '0' => '未通过', '10' => '等待审核');
    }
}


